"""
Case Type   : 系统内部使用工具
Case Name   : gs_ctl reload使用-D指定正确数据库实例目录备机是否reload成功
Create At   : 2021/01/19
Owner       : opentestcase015
Description :
    1.修改备机配置文件session_timeout参数的值
    2.gs_ctl reload指定-D设置参数为正确的数据库实例目录
    3.查看该参数是否生效
    4.恢复环境
Expect      :
    1.手动修改备机配置文件session_timeout参数的值成功
    2.执行gs_ctl reload指定-D设置参数为正确的数据库实例目录成功
    3.查看该参数的值,该参数已生效
    4.成功
History     :
    Modified by lopentestcase027 2023/1/12: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.user_node = Node('Standby1DbUser')
        self.sh_standby = CommonSH('Standby1DbUser')

    def test_system_internal_tools(self):
        LOG.info('--------step1:更改文件参数值  expect:更改成功---------')
        value_msg = self.sh_standby.execut_db_sql('show session_timeout;')
        LOG.info(value_msg)
        self.pv = value_msg.splitlines()[-2].strip()
        LOG.info(self.pv)
        self.nv = self.pv
        for arg in ('s', 'min', 'h', 'd'):
            if arg in self.pv:
                self.nv = str(int(self.pv.strip(arg)) + 1) + arg
        sed_cmd = f"sed -i 's/session_timeout = {self.pv}/session_timeout" \
            f" = {self.nv}/' {macro.DB_INSTANCE_PATH}/postgresql.conf"
        LOG.info(sed_cmd)
        sed_msg = self.user_node.sh(sed_cmd).result()
        LOG.info(sed_msg)
        LOG.info('---------校验文件参数值---------')
        check_cmd = f"cat {macro.DB_INSTANCE_PATH}/postgresql.conf|grep " \
            f"session_timeout;"
        LOG.info(check_cmd)
        check_msg = self.user_node.sh(check_cmd).result()
        LOG.info(check_msg)
        self.assertIn(self.nv, check_msg)

        LOG.info('--------step2:执行reload   expect:更改成功---------')
        msg = self.sh_standby.execute_gsctl(
            "reload", self.constant.gs_ctl_reload_success)
        self.assertTrue(msg)
        LOG.info('--------step3:查看参数是否生效   expect:生效--------')
        value_msg = self.sh_standby.execut_db_sql('show session_timeout;')
        LOG.info(value_msg)
        self.assertIn(self.nv, value_msg)

    def tearDown(self):
        LOG.info('--------step4:环境恢复    expect:成功---------')
        LOG.info('---------恢复参数值---------')
        value_cmd = f"sed -i 's/session_timeout = {self.nv}/" \
            f"session_timeout = {self.pv}/' " \
            f"{macro.DB_INSTANCE_PATH}/postgresql.conf"
        LOG.info(value_cmd)
        value_msg = self.user_node.sh(value_cmd).result()
        LOG.info(value_msg)
        msg = self.sh_standby.execute_gsctl(
            "reload", self.constant.gs_ctl_reload_success)
        restart = self.sh_standby.restart_db_cluster()
        LOG.info(restart)
        status = self.sh_standby.get_db_cluster_status(param='status')
        self.assertTrue(status)
        self.assertTrue(msg)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')
